from datetime import datetime
from urllib.parse import uses_relative

accounts={
    "admin":{"password":"123456","role":"admin"},
    "lisi":{"password":"<PASSWORD>","role":"user"}
}
finances=[
    {"id":1,"类别":"吃饭支出","账户":"交通银行","金额":"247.0","时间":"2016-03-02","说明":"家庭聚餐"},
    {"id":2,"类别":"工资收入","账户":"现金","金额":"12345.0","时间":"2016-03-15","说明":"开工资"},
    {"id":3,"类别":"服装支出","账户":"现金","金额":"1998.0","时间":"2016-04-02","说明":"买衣服"},
    {"id":4,"类别":"吃饭支出","账户":"交通银行","金额":"325.0","时间":"2016-06-18","说明":"朋友聚餐"}
]
def login():
    "用户登录验证"
    attempt=3
    while attempt > 0:
        print("\n"+"="*30)
        print("剩余登录次数：{}".format(attempt))
        username=input("请输入用户名：").strip()
        password=input("请输入密码：").strip()
        if username in accounts:
            if accounts[username]["password"]==password:
                print("\n登录成功！欢迎{}".format(username))
                return accounts[username]["role"],username
            else:
                print("密码错误！")
        else:
            print("用户名不存在！")
        attempt-=1
    print("\n登录次数超限，系统退出!")
    return None,None
def show_finances(user_role,filters=None):
    print("\n财务明细：")
    print("{:<5}{:<12}{:<8}{:<10}{:<20}{:<30}".format("ID","类别","账户","金额","时间","说明"))
    for item in finances:
        if not filters or all(item.get(k)==v for k,v in filters.items()):
            print("{:<5}{:<12}{:<8}{:<10}{:<20}{:30}".format(
                item["id"],
                item["类别"],
                item["账户"],
                item["金额"],
                item["时间"],
                item["说明"]
            ))
def add_finance():
    "添加账务"
    new_id=max(item["id"] for item in finances)+1 if finances else 1
    new_item={
        "id":new_id,
        "类别":input("请输入类别"),
        "账户":input("请输入账户"),
        "金额":input("请输入金额："),
        "时间":datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        "说明":input("请输入说明：")
    }
    finances.append(new_item)
    print("账务添加成功!")
def edit_finance():
    "编辑账务"
    show_finances("admin")
    fid=int(input("请输入要编辑的账务id:"))
    for item in finances:
        if item["id"]==fid:
            print("当前信息：")
            for k,v in item.items():
                print( "{}:{}".format(k,v))
            item["时间"]=input("新日期：") or item["时间"]
            print("账务修改成功！")
            return
        print("未找到指定的账务！")
def delete_finance():
    "删除账务"
    show_finances("admin")
    fid=int(input("请输入要删除的账务id："))
    global finances
    original_count=len(finances)
    finances=[item for item in finances if item["id"]!=fid]
    print("删除成功！" if len(finances)<original_count else"未找到指定账务！")
def user_menu(username):
    "普通用户菜单"
    while True:
        print("\n"+"="*30)
        print("欢迎进入家庭记账系统(用户：{})".format(username))
        print("1.查看所有财务")
        print("2.条件查询")
        print("3.退出系统")
        choice=input("请选择操作：").strip()
        if choice=="1":
            show_finances("user")
        elif choice=="2":
            print("\n查询条件：")
            filters={}
            if input("是否按日期查询(y/n)?")=="y":
                filters["category"]=input("请输入类型（收入/支出）：")
            show_finances("user",filters)
        elif choice=="3":
            print("感谢使用，再见！")
            break
        else:
            print("无效输入，请重新选择！")
def admin_menu():
        while True:
            print("\n"+"="*30)
            print("欢迎进入管理员系统")
            print("1.打印理财流程")
            print("2.添加账务")
            print("3.编辑账务")
            print("4.删除账务")
            print("5.查询账务")
            print("6.退出系统")
            choice =input("请选择操作：").strip()
            if choice=="1":
                show_finances("admin")
            elif choice=="2":
                add_finance()
            elif choice=="3":
                edit_finance()
            elif choice=="4":
                delete_finance()
            elif choice=="5":
                show_finances("admin")
            elif choice=="6":
                print("系统退出，再见！")
                break
            else:
                print("无效输入，请重新选择！")
def main():
        print("="*30)
        print("欢迎使用度小满家庭记账系统")
        role,username=login()
        if role=="admin":
            admin_menu()
        elif role=="user":
            user_menu(username)
if __name__=="__main__":
        main()
